package com.exchange.service;

import com.exchange.dto.GiftCardDto;
import com.exchange.entity.GiftCard;
import com.exchange.repository.GiftCardRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class GiftCardService {
    
    @Autowired
    private GiftCardRepository giftCardRepository;
    
    public Optional<GiftCardDto> findById(Long id) {
        return giftCardRepository.findById(id)
                .map(GiftCardDto::fromEntity);
    }
    
    public List<GiftCardDto> findByMerchantId(Long merchantId) {
        return giftCardRepository.findByMerchantId(merchantId)
                .stream()
                .map(GiftCardDto::fromEntity)
                .collect(Collectors.toList());
    }
    
    public GiftCardDto createGiftCard(GiftCard giftCard, Long merchantId) {
        giftCard.setMerchantId(merchantId);
        giftCard.setCreatedAt(java.time.LocalDateTime.now());
        giftCard.setUpdatedAt(java.time.LocalDateTime.now());
        
        // Generate a unique card code if not provided
        if (giftCard.getCardCode() == null || giftCard.getCardCode().isEmpty()) {
            giftCard.setCardCode(generateUniqueCardCode());
        }
        
        GiftCard savedGiftCard = giftCardRepository.save(giftCard);
        return GiftCardDto.fromEntity(savedGiftCard);
    }
    
    public GiftCardDto updateGiftCard(Long id, GiftCard giftCardDetails) {
        Optional<GiftCard> existingGiftCard = giftCardRepository.findById(id);
        if (existingGiftCard.isPresent()) {
            GiftCard giftCard = existingGiftCard.get();
            giftCard.setCardName(giftCardDetails.getCardName());
            giftCard.setDescription(giftCardDetails.getDescription());
            giftCard.setPrice(giftCardDetails.getPrice());
            giftCard.setDiscountPercentage(giftCardDetails.getDiscountPercentage());
            giftCard.setValidityStartDate(giftCardDetails.getValidityStartDate());
            giftCard.setValidityEndDate(giftCardDetails.getValidityEndDate());
            giftCard.setUpdatedAt(java.time.LocalDateTime.now());
            
            GiftCard updatedGiftCard = giftCardRepository.save(giftCard);
            return GiftCardDto.fromEntity(updatedGiftCard);
        }
        return null;
    }
    
    public boolean deleteGiftCard(Long id) {
        if (giftCardRepository.existsById(id)) {
            giftCardRepository.deleteById(id);
            return true;
        }
        return false;
    }
    
    public Optional<GiftCardDto> findByCardCode(String cardCode) {
        return giftCardRepository.findByCardCode(cardCode)
                .map(GiftCardDto::fromEntity);
    }
    
    public Optional<GiftCardDto> validateGiftCard(String cardCode) {
        Optional<GiftCard> giftCardOpt = giftCardRepository.findByCardCode(cardCode);
        
        if (giftCardOpt.isPresent() && giftCardOpt.get().isValid()) {
            return Optional.of(GiftCardDto.fromEntity(giftCardOpt.get()));
        }
        
        return Optional.empty();
    }
    
    private String generateUniqueCardCode() {
        // Generate a unique card code (in a real implementation, you might want to use a more sophisticated algorithm)
        return "GC" + System.currentTimeMillis() + java.util.UUID.randomUUID().toString().substring(0, 6).toUpperCase();
    }
}